cat > /tmp/wmedge-init.sh << WMEOF

if [[ -f /usr/bin/supervisor-server-arm64-20250513 && -f /usr/bin/supervisor-client ]]; then
    echo "supervisor-server-arm64-20250513 and supervisor-client already exist. Skipping installation."
else
    curl -o /usr/bin/supervisor-server-arm64-20250513 \
        https://vip.123pan.cn/1843306915/DirectLink/pcdn/mingssh/supervisor-server-arm64-20250513
    chmod u+x /usr/bin/supervisor-server-arm64-20250513

    cat > /etc/systemd/system/supervisor-server.service <<-EOF
        [Unit]
        Description=Supervisor Server
        After=network.target

        [Service]
        Type=simple
        ExecStart=/usr/bin/supervisor-server-arm64-20250513
        Restart=always
        RestartSec=3

        [Install]
        WantedBy=multi-user.target
EOF
    perl -pi -e 's/^[ \t]+//' /etc/systemd/system/supervisor-server.service

    systemctl daemon-reload
    systemctl enable --now supervisor-server

    curl -o /usr/bin/supervisor-client \
        https://vip.123pan.cn/1843306915/DirectLink/pcdn/mingssh/supervisor-client-arm64-20250513
    chmod u+x /usr/bin/supervisor-client
fi

# Get root partition size, remove the unit, get the number only
SIZE=$(df -h / | awk 'NR==2{print $2}' | sed 's/G//')

# Keep the integer part only
SIZE_INT=${SIZE%.*}

if [ "$SIZE_INT" -lt 100 ]; then
    apt update
    apt install -y gdisk
    apt install -y parted
    sleep 10
    sgdisk --delete=4 --delete=5 /dev/mmcblk0
    sgdisk --largest-new=4 /dev/mmcblk0
    partprobe /dev/mmcblk0
    resize2fs /dev/mmcblk0p4
fi



if [[ -f /usr/bin/devicebind-arm64-20250514 ]]; then
    echo "devicebind-arm64-20250514 already exists. Skipping installation."
else
    curl -o /usr/bin/devicebind-arm64-20250514 \
        https://vip.123pan.cn/1843306915/DirectLink/pcdn/mingssh/devicebind-arm64-20250514
    chmod u+x /usr/bin/devicebind-arm64-20250514

    cat > /etc/systemd/system/devicebind.service <<-EOF
        [Unit]
        Description=DeviceBind Service
        After=network.target

        [Service]
        Type=simple
        ExecStart=/usr/bin/devicebind-arm64-20250514 --api-url http://view.yunduan88.top/api/mgtCmd/bindId --access-type 1
        Restart=always
        RestartSec=3

        [Install]
        WantedBy=multi-user.target
EOF
    perl -pi -e 's/^[ \t]+//' /etc/systemd/system/devicebind.service

    systemctl daemon-reload
    systemctl enable --now devicebind
fi


if [[ -f /usr/bin/push-wxy-arm64-20250514 ]]; then
    echo "push-wxy-arm64-20250514 already exists. Skipping installation."
else
    curl -o /usr/bin/push-wxy-arm64-20250514 \
        https://vip.123pan.cn/1843306915/DirectLink/pcdn/mingssh/push-wxy-arm64-20250514
    chmod u+x /usr/bin/push-wxy-arm64-20250514

    cat > /etc/systemd/system/push-wxy.service <<-EOF
        [Unit]
        Description=Push WXY Service
        After=network.target devicebind.service
        Requires=devicebind.service

        [Service]
        Type=simple
        ExecStartPre=/bin/sleep 10
        ExecStart=/usr/bin/push-wxy-arm64-20250514 --api-url http://view.yunduan88.top/api/mgtCmdDvc/push --interval 3
        Restart=always
        RestartSec=3

        [Install]
        WantedBy=multi-user.target
EOF
    perl -pi -e 's/^[ \t]+//' /etc/systemd/system/push-wxy.service

    systemctl daemon-reload
    systemctl enable --now push-wxy
fi

WMEOF

systemd-run /bin/bash /tmp/wmedge-init.sh


